/*
 *  Copyright 2019-2023 oyun
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.oyun.modules.medicine.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oyun.annotation.Log;
import com.oyun.modules.medicine.domain.Doctor;
import com.oyun.modules.medicine.domain.vo.DoctorQueryCriteria;
import com.oyun.modules.medicine.service.DoctorService;
import com.oyun.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author aoyun
 * @date 2024-04-28
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "医生管理管理")
@RequestMapping("/api/doctor")
public class DoctorController {

    private final DoctorService doctorService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('doctor:list')")
    public void exportDoctor(HttpServletResponse response, DoctorQueryCriteria criteria) throws IOException {
        doctorService.download(doctorService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询医生管理")
    @ApiOperation("查询医生管理")
    @PreAuthorize("@el.check('doctor:list')")
    public ResponseEntity<PageResult<Doctor>> queryDoctor(DoctorQueryCriteria criteria, Page<Object> page) {
        return new ResponseEntity<>(doctorService.queryAll(criteria, page), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增医生管理")
    @ApiOperation("新增医生管理")
    @PreAuthorize("@el.check('doctor:add')")
    public ResponseEntity<Object> createDoctor(@Validated @RequestBody Doctor resources) {
        doctorService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改医生管理")
    @ApiOperation("修改医生管理")
    @PreAuthorize("@el.check('doctor:edit')")
    public ResponseEntity<Object> updateDoctor(@Validated @RequestBody Doctor resources) {
        doctorService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除医生管理")
    @ApiOperation("删除医生管理")
    @PreAuthorize("@el.check('doctor:del')")
    public ResponseEntity<Object> deleteDoctor(@RequestBody List<Long> ids) {
        doctorService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("上传医生图片")
    @PostMapping("/uploadProcessDefinition/{doctorId}")
    public ResponseEntity<Object> uploadOrUpdate(MultipartFile file, HttpServletRequest request, @PathVariable("doctorId") Long id) {
        return new ResponseEntity<>(doctorService.uploadOrUpdate(file, request, id), HttpStatus.OK);
    }

    @ApiOperation("根据名称查询")
    @GetMapping("/getByName")
    public ResponseEntity<Object> getByUsername(@RequestParam String name) {
        return new ResponseEntity<>(doctorService.getByName(name), HttpStatus.OK);
    }

    @ApiOperation("根据名称查询")
    @GetMapping("/getDoctorByOfficeId/{officeId}/{pageNum}/{pageSize}")
    public ResponseEntity<Object> getDoctorByOfficeId(@PathVariable("officeId") Long officeId, @PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize) {
        return new ResponseEntity<>(doctorService.getDoctorByOfficeId(officeId, pageNum, pageSize), HttpStatus.OK);
    }

}
